package com.zshan.clinic.web.controller.config;


import com.zshan.clinic.admin.entity.dto.TemplateMedicalExamDTO;
import com.zshan.clinic.admin.entity.dto.TemplateMedicalFieldDTO;
import com.zshan.clinic.admin.entity.dto.TemplateMedicalRxDTO;
import com.zshan.clinic.admin.entity.request.*;
import com.zshan.clinic.admin.entity.vo.*;
import com.zshan.clinic.admin.service.config.TemplateService;
import com.zshan.clinic.common.response.RestResp;
import com.zshan.clinic.database.model.TemplateGroup;
import com.zshan.clinic.database.model.TemplateInfo;
import com.zshan.clinic.web.controller.AbstractController;
import com.zshan.clinic.web.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 模板管理
 */
@RestController
@RequestMapping("/template")
public class TemplateController extends AbstractController {


    @Autowired
    private TemplateService templateMedicalService;

    /**
     * 查询模板组下拉框列表
     * @param
     * @return
     */
    @GetMapping("/getGroupSelectList")
    public RestResp<List<TemplateGroupSelectVo>> getGroupSelectList(@RequestParam(name = "templateType") String templateType,
                                                                    @RequestParam(name = "templateCategory") String templateCategory) {
        UserInfo userInfo = getUserInfo();
        List<TemplateGroupSelectVo> groupList = templateMedicalService.getGroupSelectList(userInfo.getClinicId(), userInfo.getEmpId(),templateCategory,templateType);
        return RestResp.SUCCESS(groupList);
    }

    /**
     * 查询模板组列表（用于首次加载）
     * @param
     * @return
     */
    @GetMapping("/getTemplateGroupList")
    public RestResp<List<TemplateGroupVo>> getTemplateGroupList(@RequestParam(name = "groupName", required = false) String groupName,
                                                                @RequestParam(name = "templateType") String templateType,
                                                                @RequestParam(name = "templateCategory") String templateCategory) {
        UserInfo userInfo = getUserInfo();
        List<TemplateGroupVo> groupList = templateMedicalService.getTemplateGroupList(userInfo.getClinicId(), groupName, userInfo.getEmpId(),templateCategory,templateType);
        return RestResp.SUCCESS(groupList);
    }


    /**
     * 添加模板组
     * @return
     */
    @PostMapping("/addGroup")
    public RestResp<Void> addGroup(@RequestBody TemplateGroup templateGroup) {
        UserInfo userInfo = getUserInfo();
        templateGroup.setCreateUserId(userInfo.getEmpId());
        templateGroup.setCreateBy(userInfo.getEmpName());
        templateGroup.setEmpId(userInfo.getEmpId());
        templateGroup.setClinicId(userInfo.getClinicId());
        templateMedicalService.addGroup(templateGroup);
        return RestResp.SUCCESS();
    }


    /**
     * 模板组移动顺序
     * @return
     */
    @PostMapping("/moveGroupSort")
    public RestResp<Void> moveGroupSort(@RequestBody MoveGroupRequest moveRequest) {
        UserInfo userInfo = getUserInfo();
        moveRequest.setUserId(userInfo.getEmpId());
        moveRequest.setUserBy(userInfo.getEmpName());
        templateMedicalService.moveGroupSort(moveRequest);
        return RestResp.SUCCESS();
    }

    /**
     * 编辑模板组
     * @return
     */
    @PostMapping("/updateGroup")
    public RestResp<Void> updateGroup(@RequestBody TemplateGroup templateGroup) {
        UserInfo userInfo = getUserInfo();
        templateGroup.setUpdateUserId(userInfo.getEmpId());
        templateGroup.setUpdateBy(userInfo.getEmpName());
        templateGroup.setEmpId(userInfo.getEmpId());
        templateGroup.setClinicId(userInfo.getClinicId());
        templateMedicalService.updateGroup(templateGroup);
        return RestResp.SUCCESS();
    }

    /**
     * 删除模板组
     * @return
     */
    @PostMapping("/deleteGroup")
    public RestResp<Void> deleteGroup(@RequestBody TemplateGroup templateGroup) {
        UserInfo userInfo = getUserInfo();
        templateGroup.setUpdateUserId(userInfo.getEmpId());
        templateGroup.setUpdateBy(userInfo.getEmpName());
        templateGroup.setEmpId(userInfo.getEmpId());
        templateGroup.setClinicId(userInfo.getClinicId());
        templateMedicalService.deleteGroup(templateGroup);
        return RestResp.SUCCESS();
    }


    /**
     * 根据模板组ID查询模板列表（用于刷新）
     * @param
     * @return
     */
    @GetMapping("/getTemplateList")
    public RestResp<List<TemplateInfoVo>> getTemplateList(@RequestParam(name = "groupId") Long groupId,
                                                          @RequestParam(name = "templateName", required = false) String templateName) {
        UserInfo userInfo = getUserInfo();
        List<TemplateInfoVo> templateList = templateMedicalService.getTemplateList(userInfo.getClinicId(),userInfo.getEmpId(), groupId,templateName);
        return RestResp.SUCCESS(templateList);
    }

    /**
     * 添加模板
     * @return
     */
    @PostMapping("/addTemplate")
    public RestResp<Void> addTemplate(@RequestBody TemplateInfo template) {
        UserInfo userInfo = getUserInfo();
        template.setCreateUserId(userInfo.getEmpId());
        template.setCreateBy(userInfo.getEmpName());
        template.setClinicId(userInfo.getClinicId());
        template.setEmpId(userInfo.getEmpId());
        templateMedicalService.addTemplate(template);
        return RestResp.SUCCESS();
    }

    /**
     * 模板移动顺序
     * @return
     */
    @PostMapping("/moveTemplateSort")
    public RestResp<Void> moveTemplateSort(@RequestBody MoveTemplateSortRequest moveTemplateRequest) {
        UserInfo userInfo = getUserInfo();
        moveTemplateRequest.setUserId(userInfo.getEmpId());
        moveTemplateRequest.setUserBy(userInfo.getEmpName());
        moveTemplateRequest.setClinicId(userInfo.getClinicId());
        templateMedicalService.moveTemplateSort(moveTemplateRequest);
        return RestResp.SUCCESS();
    }

    /**
     * 移动模板到其他目录
     * @return
     */
    @PostMapping("/moveTemplate")
    public RestResp<Void> moveTemplate(@RequestBody MoveTemplateRequest moveTemplateRequest) {
        UserInfo userInfo = getUserInfo();
        moveTemplateRequest.setUserId(userInfo.getEmpId());
        moveTemplateRequest.setUserBy(userInfo.getEmpName());
        moveTemplateRequest.setClinicId(userInfo.getClinicId());
        templateMedicalService.moveTemplate(moveTemplateRequest);
        return RestResp.SUCCESS();
    }


    /**
     * 共享模板
     * @return
     */
    @PostMapping("/shareTemplate")
    public RestResp<Void> shareTemplate(@RequestBody TemplateInfo template) {
        UserInfo userInfo = getUserInfo();
        template.setUpdateUserId(userInfo.getEmpId());
        template.setUpdateBy(userInfo.getEmpName());
        template.setClinicId(userInfo.getClinicId());
        template.setEmpId(userInfo.getEmpId());
        templateMedicalService.shareTemplate(template);
        return RestResp.SUCCESS();
    }


    /**
     * 取消共享模板
     * @return
     */
    @PostMapping("/unshareTemplate")
    public RestResp<Void> unshareTemplate(@RequestBody TemplateInfo template) {
        UserInfo userInfo = getUserInfo();
        template.setUpdateUserId(userInfo.getEmpId());
        template.setUpdateBy(userInfo.getEmpName());
        template.setClinicId(userInfo.getClinicId());
        template.setEmpId(userInfo.getEmpId());
        templateMedicalService.unshareTemplate(template);
        return RestResp.SUCCESS();
    }




    /**
     * 拉取模板到个人目录
     * @return
     */
    @PostMapping("/fetchTemplate")
    public RestResp<Void> fetchTemplate(@RequestBody FetchTemplateRequest template) {
        UserInfo userInfo = getUserInfo();
        template.setUserId(userInfo.getEmpId());
        template.setUserBy(userInfo.getEmpName());
        template.setClinicId(userInfo.getClinicId());
        templateMedicalService.fetchTemplate(template);
        return RestResp.SUCCESS();
    }

    /**
     * 复制模板
     * @return
     */
    @PostMapping("/copyTemplate")
    public RestResp<Void> copyTemplate(@RequestBody CopyTemplateRequest template) {
        UserInfo userInfo = getUserInfo();
        template.setUserId(userInfo.getEmpId());
        template.setUserBy(userInfo.getEmpName());
        template.setClinicId(userInfo.getClinicId());
        templateMedicalService.copyTemplate(template);
        return RestResp.SUCCESS();
    }

    /**
     * 删除模板
     * @return
     */
    @PostMapping("/deleteTemplate")
    public RestResp<Void> deleteTemplate(@RequestBody TemplateInfo template) {
        UserInfo userInfo = getUserInfo();
        template.setUpdateUserId(userInfo.getEmpId());
        template.setUpdateBy(userInfo.getEmpName());
        template.setClinicId(userInfo.getClinicId());
        template.setEmpId(userInfo.getEmpId());
        templateMedicalService.deleteTemplate(template);
        return RestResp.SUCCESS();
    }


    /**
     * 查询病历模板详情
     * @param
     * @return
     */
    @GetMapping("/getTemplateMedicalDetail")
    public RestResp<TemplateMedicalFieldVo> getTemplateMedicalDetail(@RequestParam(name = "templateId") Long templateId) {
        UserInfo userInfo = getUserInfo();
        TemplateMedicalFieldVo templateMedicalFieldVo = templateMedicalService.getTemplateMedicalDetail(userInfo.getClinicId(),templateId);
        return RestResp.SUCCESS(templateMedicalFieldVo);
    }

    /**
     * 门诊：保存病历模板
     * @return
     */
    @PostMapping("/addTemplateMedical")
    public RestResp<Void> addTemplateMedical(@RequestBody TemplateMedicalFieldDTO templateMedicalFieldDTO) {
        UserInfo userInfo = getUserInfo();
        templateMedicalFieldDTO.setCreateUserId(userInfo.getEmpId());
        templateMedicalFieldDTO.setCreateBy(userInfo.getEmpName());
        templateMedicalFieldDTO.setClinicId(userInfo.getClinicId());
        templateMedicalFieldDTO.setEmpId(userInfo.getEmpId());
        templateMedicalService.addTemplateMedicalDTO(templateMedicalFieldDTO);
        return RestResp.SUCCESS();
    }

    /**
     * 编辑病历模板
     * @return
     */
    @PostMapping("/updateTemplateMedical")
    public RestResp<Void> updateTemplateMedical(@RequestBody TemplateMedicalFieldDTO templateMedicalFieldDTO) {
        UserInfo userInfo = getUserInfo();
        templateMedicalFieldDTO.setUpdateUserId(userInfo.getEmpId());
        templateMedicalFieldDTO.setUpdateBy(userInfo.getEmpName());
        templateMedicalFieldDTO.setClinicId(userInfo.getClinicId());
        templateMedicalFieldDTO.setEmpId(userInfo.getEmpId());
        templateMedicalService.updateTemplateMedicalDTO(templateMedicalFieldDTO);
        return RestResp.SUCCESS();
    }



    /**
     * 查询查验模板详情
     * @param
     * @return
     */
    @GetMapping("/getTemplateExamDetail")
    public RestResp<TemplateMedicalExamVo> getTemplateExamDetail(@RequestParam(name = "templateId", required = false) Long templateId) {
        UserInfo userInfo = getUserInfo();
        TemplateMedicalExamVo templateMedicalExamVo = templateMedicalService.getTemplateExamDetail(userInfo.getClinicId(),templateId);
        return RestResp.SUCCESS(templateMedicalExamVo);
    }

    /**
     * 门诊：保存查验模板
     * @return
     */
    @PostMapping("/addTemplateExam")
    public RestResp<Void> addTemplateExam(@RequestBody TemplateMedicalExamDTO templateMedicalExamDTO) {
        UserInfo userInfo = getUserInfo();
        templateMedicalExamDTO.setCreateUserId(userInfo.getEmpId());
        templateMedicalExamDTO.setCreateBy(userInfo.getEmpName());
        templateMedicalExamDTO.setClinicId(userInfo.getClinicId());
        templateMedicalExamDTO.setEmpId(userInfo.getEmpId());
        templateMedicalService.addTemplateExamDTO(templateMedicalExamDTO);
        return RestResp.SUCCESS();
    }

    /**
     * 编辑查验模板
     * @return
     */
    @PostMapping("/updateTemplateExam")
    public RestResp<Void> updateTemplateExam(@RequestBody TemplateMedicalExamDTO templateMedicalExamDTO) {
        UserInfo userInfo = getUserInfo();
        templateMedicalExamDTO.setUpdateUserId(userInfo.getEmpId());
        templateMedicalExamDTO.setUpdateBy(userInfo.getEmpName());
        templateMedicalExamDTO.setClinicId(userInfo.getClinicId());
        templateMedicalExamDTO.setEmpId(userInfo.getEmpId());
        templateMedicalService.updateTemplateExamDTO(templateMedicalExamDTO);
        return RestResp.SUCCESS();
    }


    /**
     * 查询处方模板详情
     * @param templateId
     * @return
     */
    @GetMapping("/getTemplateRxDetail")
    public RestResp<TemplateMedicalRxVo> getTemplateRxDetail(@RequestParam(name = "templateId") Long templateId) {
        UserInfo userInfo = getUserInfo();
        TemplateMedicalRxVo templateMedicalRxVo = templateMedicalService.getTemplateRxDetail(userInfo.getClinicId(),templateId);
        return RestResp.SUCCESS(templateMedicalRxVo);
    }

    /**
     * 门诊：保存处方模板
     * @return
     */
    @PostMapping("/addTemplateRx")
    public RestResp<Void> addTemplateRx(@RequestBody TemplateMedicalRxDTO templateMedicalRxDTO) {
        UserInfo userInfo = getUserInfo();
        templateMedicalRxDTO.setCreateUserId(userInfo.getEmpId());
        templateMedicalRxDTO.setCreateBy(userInfo.getEmpName());
        templateMedicalRxDTO.setClinicId(userInfo.getClinicId());
        templateMedicalRxDTO.setEmpId(userInfo.getEmpId());
        templateMedicalService.addTemplateRxDTO(templateMedicalRxDTO);
        return RestResp.SUCCESS();
    }

    /**
     * 编辑处方模板
     * @return
     */
    @PostMapping("/updateTemplateRx")
    public RestResp<Void> updateTemplateRx(@RequestBody TemplateMedicalRxDTO templateMedicalRxDTO) {
        UserInfo userInfo = getUserInfo();
        templateMedicalRxDTO.setUpdateUserId(userInfo.getEmpId());
        templateMedicalRxDTO.setUpdateBy(userInfo.getEmpName());
        templateMedicalRxDTO.setClinicId(userInfo.getClinicId());
        templateMedicalRxDTO.setEmpId(userInfo.getEmpId());
        templateMedicalService.updateTemplateRxDTO(templateMedicalRxDTO);
        return RestResp.SUCCESS();
    }
}
